<?php 

session_start();
/*
if(isset($_POST['function_name'])&&$_POST['function_name']!=null) {
	$_POST['function_name']($_POST['params']['role'],$_POST['params']['passwd']);
}*/

if (isset($_REQUEST['maFonction']) && $_REQUEST['maFonction'] != '')
{
    $_REQUEST['maFonction']($_REQUEST);
}

//function authenticate($role, $password){
function authenticate($data){	

	$role = $data['params']['role'];
	$mdp=$data['params']['passwd'];
	
	$host = "localhost"; 
	$user = "postgres"; 
	//A changer selon la base
	$pass = "admin"; 
	$db = "postgres"; 

	$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
		or die ("Probleme de connexion au serveur\n"); 


	$query = "SELECT * FROM authentication WHERE role='$role' AND password='$mdp';"; 
	$rs = pg_query($con, $query) or die("Probleme avec la requete suivante: $query\n"); 

	if($row = pg_fetch_row($rs)) {
		pg_close($con);
		getRights($role);
	} else {
		echo "Connexion impossible : votre role et mot de passe ne correspondent pas.";
	}
}

function getRights($login) {
	$host = "localhost"; 
	$user = "postgres"; 
	//A changer selon la base
	$pass = "admin"; 
	$db = "postgres"; 

	$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
		or die ("Probleme de connexion au serveur\n");  
	  
	$query = "SELECT access_rights FROM authentication WHERE role='$login';";
	$result = pg_query($con, $query) or die("Cannot execute query: $query\n");
	if($row = pg_fetch_row($result)) {
		
		$_SESSION['user'] = $login;
		$_SESSION['droits'] = $row[0];
/*		
		if($row[0][0] == 1) $_SESSION['gonogoR']="enabled";
		else $_SESSION['gonogoR']="disabled";
		if($row[0][1] == 1) $_SESSION['decisionR']="enabled";
		else $_SESSION['decisionR']="disabled";
		if($row[0][2] == 1) $_SESSION['wikiR']="enabled";
		else $_SESSION['wikiR']="disabled";
		if($row[0][3] == 1) $_SESSION['fopR']="enabled";
		else $_SESSION['fopR']="disabled";
		if($row[0][4] == 1) $_SESSION['tmR']="enabled";
		else $_SESSION['tmR']="disabled";
		if($row[0][5] == 1) $_SESSION['logR']="enabled";
		else $_SESSION['logR']="disabled";
*/		
		echo $_SESSION['user'];

	} else {
		echo "Probleme de recuperation des droits";
	}
	
}
	
?>
